<?php
//货品列表控制器类
class GoodsListController extends CommonController{
    private $model;
    public function __init(){
        parent::__init();
        $this->model=K("GoodsList");
    }
    //动作方法
    //
    public function index(){
        // p($_GET);
        // 获取商品ID
        $gid = Q('get.gid',0,'intval');
        // 通过GID查询出已经存在的货品
        $listData = $this->model->get_data(array('gid'=>$gid));
        // 为货品相对应的属性查询商品属性名
        foreach ($listData as $k => $v){   
            $where = array('gaid'=>array('in',$v['combine']));
            $listData[$k]['spec'] = K('GoodsAttr')->where($where)->getField('gtvalue', true);
        }
        // p($listData);
        // 分配变量,已存在商品属性
        $this->assign('listData',$listData);
        // 获取类型ID
        $tid = Q('get.tid',0,'intval');
        $class = "1";
        // 查询出所属类型下的商品属性
        $AttrData = K('TypeAttr')->get_data(array('tid'=>$tid,'class'=>$class));
        //增加所需要的规格选择
        $field = array('gaid', 'gtvalue');
        foreach ($AttrData as $k => $v)
        {
            $where = array('gid' => $gid, 'taid' => $v['taid']);
            $AttrData[$k]['opt'] = K('GoodsAttr')->field($field)->where($where)->select();
        }
        // p($AttrData);die();
        // 分配变量商品属性
        $this->assign('AttrData',$AttrData);
        //显示视图
        $this->display();
    }
    public function add(){
        if(IS_POST){
            // p($_POST);die();
            if($this->model->create()){
                $spec =Q('post.spec',0,"intval");
                $gid =Q('post.gid',0,"intval");
                $gidInfo = K('Goods')->where(array('gid'=>$gid))->find();
                $added = $gidInfo['shopprice'];
                if ($spec) {
                    foreach ($spec as $key => $v) {
                        $attrData =K('GoodsAttr')->where(array('gaid'=>$v))->find();
                        $add =$attrData['added'];
                        $added +=$add;
                    }
                }
                $this->model->add_data($added);
                $this->success("增加成功",U("Goods/index"));
            }
            $this->error($this->model->error); 
        }
    }
    /**
     * [getAttr 异步获取类型数据]
     * @return [type] [description]
     */
    public function getAttr(){
    	if(!IS_AJAX) $this->error('非法请求');
    	$tid = Q('post.tid');
    	$AttrData=K('TypeAttr')->get_data(array('tid'=>$tid));
    	// p($AttrData);die();
    	if ($AttrData) {
    		echo json_encode($AttrData);
    		die();
    	}else{
    		echo 0;
    		die();
    	}
    	
    }
    /**
     * [del 删除货品]
     * @return [type] [description]
     */
    public function del(){
        $glid =Q('get.glid',0, 'intval');
        // p($gid);die();
        if($this->model->del_data(array('glid'=>$glid))){
            $this->success('删除成功');
        }else{
            $this->error($this->model->error);
        }
    }

    public function edit(){
        if (IS_POST) {
            $glid = Q('post.glid',0,"intval");
            $this->model->update_data(array('glid'=>$glid));
            $this->success('修改成功',U("Goods/index"));
        }
        $gid = Q('get.gid',0,'intval');
        $glid = Q('get.glid',0,"intval");
        // p($glid);die();
        $listData = $this->model->get_data(array('glid'=>$glid));
        // 为货品相对应的属性查询商品属性名
        foreach ($listData as $k => $v){   
            $where = array('gaid'=>array('in',$v['combine']));
            $listData[$k]['spec'] = K('GoodsAttr')->where($where)->getField('gtvalue', true);
        }
        // p($listData);
        // 分配变量,已存在商品属性
        $this->assign('listData',$listData);
        // 获取类型ID
        $tid = Q('get.tid',0,'intval');
        $class = "1";
        // 查询出所属类型下的商品属性
        $AttrData = K('TypeAttr')->get_data(array('tid'=>$tid,'class'=>$class));
        //增加所需要的规格选择
        $field = array('gaid', 'gtvalue');
        foreach ($AttrData as $k => $v)
        {
            $where = array('gid' => $gid, 'taid' => $v['taid']);
            $AttrData[$k]['opt'] = K('GoodsAttr')->field($field)->where($where)->select();
        }
        // p($AttrData);die();
        // 分配变量商品属性
        $this->assign('AttrData',$AttrData);
        // 显示视图
        $this->display();
    }

    /**
     * 异步检测货品是否已存在
     */
    public function check(){
        $gid = Q('post.gid',0,"intval");
        $combine = implode(',', Q('post.spec',0,"intval"));
        $where = array('gid' =>$gid, 'combine' => $combine);
        // p($where);die();
        echo K('GoodsList')->where($where)->count() ? 0 : 1;
        die();
    }
}
